package com.example.user.controller;

import model.ExportModel;
import model.ImportModel;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import utils.ExcelUtil;
import java.util.Arrays;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * @ClassName ExcelTestController
 * @Description
 * @Author 朱正新
 * @Version 1.0.0
 * @Date 2021/4/1 21:25
 */
@RestController
@RequestMapping(value = "/easyExcel")
public class ExcelTestController {

    @PostMapping(value = "/import")
    public List<ImportModel> read(MultipartFile excel) {
        return ExcelUtil.readExcel(excel, ImportModel.class, 0);
    }

    @GetMapping(value = "/export")
    public void writeExcel(HttpServletResponse response) {
        List<ExportModel> list = getList();
        String fileName = "Excel导出测试";
        String sheetName = "sheet1";

        sheetName.hashCode();

        ExcelUtil.writeDynamicHeadExcel(response, list, fileName, sheetName, ExportModel.class, head());
    }
    @GetMapping(value = "/export2")
    public void writeExcel2(HttpServletResponse response) {
        List<ExportModel> list = getList();
        String fileName = "Excel导出测试2";
        String sheetName = "sheet1";
        ExcelUtil.writeExcel(response, list, fileName, sheetName, ExportModel.class);
    }

    private List<ExportModel> getList() {
        List<ExportModel> modelList = new ArrayList<>();

        ExportModel firstModel = new ExportModel();
        firstModel.setName("李明");
        firstModel.setSex("男");
        firstModel.setAge(20);
        modelList.add(firstModel);

        ExportModel secondModel = new ExportModel();
        secondModel.setName("珍妮");
        secondModel.setSex("女");
        secondModel.setAge(19);
        modelList.add(secondModel);

        return modelList;
    }

    private List<List<String>> head() {
        List<List<String>> headList = new ArrayList<>();
        List<String> nameHead = new ArrayList<>();
        nameHead.add("姓名");
        List<String> genderHead = new ArrayList<>();
        genderHead.add("性别");
        List<String> ageHead = new ArrayList<>();
        ageHead.add("年龄");
        headList.add(nameHead);
        headList.add(genderHead);
        headList.add(ageHead);

        return headList;
    }

    public static void main(String[] args) {
      int [] nums = {0,0,1,1,1,2,2,3,3,4};
      //int [] nums = {0,1,2,3,4};

        int i = 0;
        for (int j = 1; j < nums.length; j++) {
            System.out.println(nums[j]+"-----"+nums[i]);
            if (nums[j] != nums[i]) {
                i++;
                nums[i] = nums[j];
            }
        }
        System.out.println(i);
        System.out.println(Arrays.toString(nums));
    }
}
